<template>
  <a-layout>
    <a-layout-content :style="{ background: '#e2e2e2', padding: '24px', margin: 0, minHeight: '280px'}">
      <a-row>
        <a-col :span="4">
          <a-tree
              v-if="level1.length > 0"
              :tree-data="level1"
              @select="onSelect"
              :replaceFields="{title: 'name', key: 'id', value: 'id'}"
              :defaultExpandAll="true"
              :defaultSelectedKeys="defaultSelectedKeys"
          >
          </a-tree>
        </a-col>
        <a-col :span="20" >
          <a-page-header :title="doc.name" style="background-color: #f5f5f5; padding: 24px;">
            <a-row type="flex">
              <a-space size = "large">
                <a-statistic title="阅读数" :value="doc.viewCount" />
                <a-statistic title="点赞数" :value="doc.voteCount" />
              </a-space>
            </a-row>
          </a-page-header>
          <div class="wangeditor" :innerHTML="html" :style="{ padding: '30px' }"></div>
          <a-divider orientation="left" style="background-color: white; padding: 24px; margin: 0 0;">
            <a-space>
              <a-button @click="vote" type="primary" shape="round" size="large">
                <template #icon><LikeOutlined/></template>
                点赞{{doc.voteCount}}
              </a-button>
              <a-button type="primary" shape="round" size="large">
                <template #icon><StarOutlined /></template>
                收藏
              </a-button>
            </a-space>
          </a-divider>
        </a-col>
      </a-row>

    </a-layout-content>

  </a-layout>

</template>

<script lang="ts">
import {defineComponent, onMounted, ref, createVNode} from 'vue';
import axios from 'axios'
import {message} from "ant-design-vue";
import {Tool} from "@/util/tool";
import {useRoute} from "vue-router";
import {Modal} from 'ant-design-vue';
import {ExclamationCircleOutlined} from '@ant-design/icons-vue';
import E from 'wangeditor'

export default defineComponent({
  name: 'Doc',
  setup() {
    const docs = ref();
    const route = useRoute();
    const html = ref();
    const defaultSelectedKeys = ref();
    defaultSelectedKeys.value = [];
    //当前选中文档
    const doc = ref();
    doc.value = {};

    /**
     * 一级文档树，children属性就是二级文档
     * [{
     *   id: "",
     *   name: "",
     *   children:[{
     *     id: "",
     *     name: "",
     *   }]
     * }]
     */
    const level1 = ref();  //一级文档树，children属性就是二级文档
    level1.value = [];

    /**
     * 内容查询
     **/
    const handleQueryContent = (id: number) => {
      axios.get("/doc/find-content/" + id).then((response) => {
        const data = response.data;
        if (data.success) {
          doc.value.viewCount++;
          html.value = data.content;
        } else {
          message.error(data.message);
        }
      })
    };

    /**
     * 数据查询
     **/
    const handleQuery = () => {
      axios.get("/doc/all/" + route.query.ebookId).then((response) => {
        const data = response.data;
        if (data.success) {
          docs.value = data.content;
          level1.value = [];
          level1.value = Tool.array2Tree(docs.value, 0);
          if (Tool.isNotEmpty(level1)) {
            defaultSelectedKeys.value = [level1.value[0].id]
            handleQueryContent(level1.value[0].id)
            //初始显示文档信息
            doc.value = level1.value[0];
          }
        } else {
          message.error(data.message);
        }
      })
    };

    const onSelect = (selectedKeys: any, info: any) => {
      console.log('selected', selectedKeys, info);
      if (Tool.isNotEmpty(selectedKeys)) {
        //选中某一节点时，加载该节点的文档信息
        doc.value = info.selectedNodes[0].props;
        //加载内容
        handleQueryContent(selectedKeys[0]);

      }
    };
    const vote = () =>{
      axios.get('/doc/vote/' + doc.value.id).then((response) => {
        const data = response.data;
        if (data.success) {
          doc.value.voteCount++;
        }else {
          message.error(data.message);
        }
      });
    }
    onMounted(() => {
      handleQuery();
    });

    return {
      level1,
      html,
      onSelect,
      vote,
      defaultSelectedKeys,
      doc
    }
  }
});
</script>

<style>
/* wangeditor 默认样式 */
/* table 样式 */
.wangeditor table {
  border-top: 1px solid #ccc;
  border-left: 1px solid #ccc;
}
.wangeditor{
  background: white;
}

.wangeditor table td,
.wangeditor table th {
  border-bottom: 1px solid #ccc;
  border-right: 1px solid #ccc;
  padding: 3px 5px;
}

.wangeditor table th {
  border-bottom: 2px solid #ccc;
  text-align: center;
}

/* blockquote 样式 */
.wangeditor blockquote {
  display: block;
  border-left: 8px solid #d0e5f2;
  padding: 5px 10px;
  margin: 10px 0;
  line-height: 1.4;
  font-size: 100%;
  background-color: #f1f1f1;
}

/* code 样式 */
.wangeditor code {
  display: inline-block;
  *display: inline;
  *zoom: 1;
  background-color: #f1f1f1;
  border-radius: 3px;
  padding: 3px 5px;
  margin: 0 3px;
}

.wangeditor pre code {
  display: block;
}

/* ul ol 样式 */
.wangeditor ul, ol {
  margin: 10px 0 10px 20px;
}
.wangeditor h1 {
  font-size: 2em;
}
.wangeditor body{
  font-size: 14px;
}
.wangeditor blockquote{
  font-size: 16px !important;
  font-weight: 600;
  padding: 20px !important;
}
.ant-page-header-heading-title{
  font-size: 2.2em;
  height: 50px;
}
.ant-tree li .ant-tree-node-content-wrapper.ant-tree-node-selected {
  background-color: #e2e2e2;
  color: dodgerblue;
}
.wangeditor img {
  max-width: 100%;
  height: auto;
}
</style>